package cn.zwx.goods.svc.utils;

import cn.zwx.common.redisson.utils.RedisUtils;
import cn.zwx.goods.svc.constans.RedisKeyConstans;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author zhangwenxue
 */
@Component
public class SpuCodeGeneratorUtil {
    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyyMMdd");

    @Resource
    private RedisUtils redisUtils;

    public String generateProductCode(String productCategory, String batchNumber,LocalDate productionDate) {
        String date = productionDate.format(DTF);
        String key = RedisKeyConstans.SPU_CODE_REDIS_KEY + productCategory + ":" + batchNumber + ":" + date;
        // 原子性地递增序列号
        long seq = redisUtils.incrAtomicValue(key);
        // 组合成最终的商品编码
        return String.format("%s%s%s%04d", productCategory, batchNumber, date, seq);
    }


}
